
<?php
error_reporting('IGNORE_REPEATED_ERRORS');
$updated_procedure_title = str_replace(array("\n", "\r", "\o", "\xOB"), '', trim(nl2br($_POST["updated_procedure_title"])));
$updated_procedure_body = str_replace(array("\n", "\r", "\o", "\xOB"), '', trim(nl2br($_POST["updated_procedure_body"])));
$updated_publish_date = $_POST["updated_publish_date"];
$updated_purge_date = $_POST["updated_purge_date"];
$procedure_title = $_POST["procedure_title"];
$new_procedure_title = "newtitle";
?>

<HTML>
<HEAD>
<script type="text/javascript">
function update(index){
	if (isNaN(index)) {
		document.getElementById("procedures_manager").reset();
	}else{
		document.getElementById("procedures_manager").updated_procedure_title.value = title[index];
		document.getElementById("procedures_manager").updated_procedure_body.value = body[index];
	}
}
</script>
</HEAD>
<BODY>
<FORM action="./manage_procedures.php" method=POST name=procedures_manager id=procedures_manager>
<B>This form allows you to change the procedures in the system.</B><BR><BR>



<?php

//echo "<BR><BR>"; 
//echo "1) $procedure_title <BR> 2) $updated_procedure_title <BR> 3) $updated_procedure_body <BR> 4) $updated_publish_date <BR> 5) $updated_purge_date";
//echo "<BR><BR>";

mysql_connect("localhost", "root", "") or
    die("Could not connect: " . mysql_error());
mysql_select_db("QA");

if( (strlen($procedure_title)>0) && (strlen($updated_procedure_title)>0) && (strlen($updated_procedure_body)>0) ){
	if( strcmp($procedure_title, $new_procedure_title)==0 ){
		mysql_query("insert into QA.procedures (Title, Body) VALUES (\"$updated_procedure_title\", \"$updated_procedure_body\")");
		echo "<I>New procedure Created \"$updated_procedure_title\".</I><BR><BR>";
	}else{
		mysql_query("update QA.procedures SET Title=\"".$updated_procedure_title."\", Body=\"".$updated_procedure_body."\" where Title=\"".$current_procedure_title."\"");
		echo "<I>Updated ".$current_procedure_title." to ".$updated_procedure_title.".</I><BR><BR>";
	}
}




$result = mysql_query("select Distinct Title, Body from QA.procedures");
$titles = Array();
$body = Array();
$publish = Array();
$purge = Array();
$i = -1;
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
	$i = $i + 1;
	$title[$i] = $row[0];
	$body[$i] = $row[1];
	$publish[$i] = $row[2];
	$purge[$i] = $row[3];
}

mysql_free_result($result);


echo "<SCRIPT type=\"text/javascript\">\r\n<!--\r\n";
echo "var title = new Array();\r\n";
echo "var body = new Array();\r\n";
echo "var publish = new Array();\r\n";
echo "var purge = new Array();\r\n";
$j = 0;
while ($j<=$i){
	echo "title[$j] = \"$title[$j]\";\r\n";
	echo "body[$j] = \"$body[$j]\";\r\n";
	echo "publish[$j] = \"$publish[$j]\";\r\n";
	echo "purge[$j] = \"$purge[$j]\";\r\n";
	$j = $j+1;
}
echo "-->\r\n</SCRIPT>\r\n<BR>";


echo "Current procedures (Select one to view/update, to create a new one select \"New procedure\")<BR>";
echo "<select name=current_procedure_index id=current_procedure_index onBlur=\"javascript:update(current_procedure_index.value);\">\r\n";
echo "<option value=\"$new_procedure_title\">New procedure</OPTION>\r\n";
$i = 0;
foreach($title as $current) {
	echo "<OPTION value=\"".$i."\">".$current."</OPTION>\r\n";
	$i = $i + 1;
}
echo "</SELECT>\r\n<BR>";
echo "<INPUT type=\"hidden\" name=procedure_title id=procedure_title value=\"$new_procedure_title\"/>\r\n";
?>
<BR>
Title: 
<TEXTAREA size=255 rows=2 cols=100 name=updated_procedure_title id=updated_procedure_title></TEXTAREA><BR>
Body:
<TEXTAREA size=2048 rows=20 cols=100 name=updated_procedure_body id=updated_procedure_body></TEXTAREA><BR>
<BR>
<INPUT type=SUBMIT name=SUBMIT /><INPUT type=RESET name=RESET />
</FORM>
<BODY>
</HTML>